package com.entfrm.biz.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.base.api.R;
import com.entfrm.biz.system.entity.TProduct;
import com.entfrm.biz.system.entity.User;
import com.entfrm.biz.system.service.ProductService;
import com.entfrm.biz.system.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/product")
public class ProductController  {
    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;

    private QueryWrapper<TProduct> getQueryWrapper() {
        return new QueryWrapper<TProduct>().orderByDesc("product_id");
    }
    /**
     * 根据ID更改上下架
     * @param id 商品编号
     * @param ground 上下架状态
     */
    @ResponseBody
    @RequestMapping("ground")
    public R ground(String id,String ground ){
        try {
            productService.updateGround(id,ground);
        }catch (Exception e){
            return R.error("msg","失败");
        }
        return R.ok("msg","成功");
    }

    /**
     * 更改商品的归属
     * @param productId 商品编号
     * @param userId 新用户编号
     */
    @ResponseBody
    @RequestMapping("changeOwn")
    public R changeOwn(String productId,String userId ){
        try {
            //查询用户信息
           User user = userService.getById(userId);
           //更改用户信息
            productService.changeOwn(userId,productId,user.getUserName());
        }catch (Exception e){
            return R.error("msg","失败");
        }
        return R.ok("msg","成功");
    }
    @GetMapping("/list")
    public R list(Page page,String current){
        User user = userService.getById("8");
        IPage<TProduct> productIPage = null;
        try {
             productIPage = productService.page(page,getQueryWrapper());

        }catch (Exception e){
            e.printStackTrace();
        }
        return R.ok(productIPage.getRecords(), productIPage.getTotal());
    }
}
